package self.hoy.demo.rabbitmq.confirm;

import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import self.hoy.demo.rabbitmq.Constant;
import self.hoy.demo.rabbitmq.util.ConnectionUtils;

import java.io.IOException;
import java.util.concurrent.TimeoutException;

/**
 * @author hyh
 * @date 2020/6/7
 */
public class BatchSend implements Constant {
    public static void main(String[] args) throws IOException, TimeoutException, InterruptedException {
        Connection connection = ConnectionUtils.getConnection();
        Channel channel = connection.createChannel();
        channel.queueDeclare(QUEUE_NAME, false, false, false, null);
        //生产者调用confirmSelect 将channel设置为confirm模式
        channel.confirmSelect();
        for (int i = 0; i < 10; i++) {
            String msg = "batch " + i + " sending Hello World!";
            channel.basicPublish("", QUEUE_NAME, null, msg.getBytes());
        }
        if (channel.waitForConfirms()){
            System.out.println("send success");
        } else {
            System.out.println("send error");
        }
        channel.close();
        connection.close();
    }
}
